package ${packageName};

import android.os.Bundle;

import com.dee.components.base.ui.JumpOptions;

<#if applicationPackage??>
import ${applicationPackage}.R;
import ${applicationPackage}.base.BaseActivity;</#if>

<#if !generatedFragment>

<#if injectionEntity>
import ${packageName}.entity.${EntityName};</#if>

<#if injectionMVP>
import ${packageName}.contract.${ContractName};
import ${packageName}.presenter.${PresenterName};
import com.dee.components.util.ToastTool;
import com.dee.components.util.DataUtils;</#if>

<#if injectionRecyclerView>
import androidx.annotation.NonNull;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.dee.components.adapter.base.BaseQuickAdapter;
import com.dee.components.adapter.base.BaseViewHolder;
import com.dee.components.smartrefresh.header.ClassicsHeader;
import com.dee.components.smartrefresh.SmartRefreshLayout;
import com.dee.components.smartrefresh.api.RefreshLayout;
import com.dee.components.smartrefresh.listener.OnRefreshLoadMoreListener;
import com.dee.components.util.ImageUtils;
import com.dee.components.widget.ItemDecoration;
import com.dee.components.listener.OnNoFastClickListener;</#if>

<#if injectionTitleBar>
import com.dee.components.widget.TitleView;</#if>
<#if injectionTitleBar||injectionRecyclerView>
import butterknife.BindView;</#if>

<#else>

<#if injectionTitleBar>
import com.dee.components.widget.TitleView;
import butterknife.BindView;</#if>
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
</#if>
<#if generatedDrawerFragment>
import com.dee.components.listener.OnNoFastClickListener;
import com.dee.components.util.KeyboardUtils;
import androidx.annotation.NonNull;
import android.view.View;
<#if !generatedFragment>
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;</#if>
import android.widget.FrameLayout;
import androidx.drawerlayout.widget.DrawerLayout;

<#if !injectionTitleBar&&!injectionRecyclerView>
import butterknife.BindView;</#if>
</#if>
/**
 * Created by Liu on 2020/4/19.
 * 当前类注释:
 *  ${activityTitle}
 *
 */
public class ${ActivityName} extends BaseActivity<#if injectionMVP && !generatedFragment><${PresenterName}> implements ${ContractName}.${IViewName}</#if>{

	<#if injectionTitleBar>
    @BindView(R.id.toolbar_title_view)
    TitleView toolbarTitleView;</#if>
<#if !generatedFragment>
	<#if injectionRecyclerView>
    @BindView(R.id.${layout_acname}_recycler_view)
    RecyclerView ${lower_acname}RecyclerView;
    @BindView(R.id.${layout_acname}_smart_refresh_layout)
    SmartRefreshLayout ${lower_acname}SmartRefreshLayout;</#if>
</#if>
<#if generatedDrawerFragment>
    @BindView(R.id.${layout_acname}_nav_view)
    FrameLayout ${lower_acname}NavView;
    @BindView(R.id.${layout_acname}_drawer)
    DrawerLayout ${lower_acname}Drawer;
</#if>
    public static JumpOptions startJump(String datatype) {
        return new JumpOptions()
                //.setBundle(AppConstant.INTENT_DATATYPE, datatype)
                ;
    }
 
    @Override
    public int getLayoutId() {
        return R.layout.${activity_layout_name};
    }

    @Override
    public void initPresenter() {
		//简单页面无需mvp就不用管此方法即可
		<#if !generatedFragment>
		<#if injectionMVP>
        mPresenter.setVM(this);
		</#if></#if>
    }
<#if !generatedFragment>
	<#if injectionRecyclerView>
		<#if injectionEntity>
		
    private BaseQuickAdapter<${EntityName}, BaseViewHolder> ${lower_acname}Adapter;
		<#else>
		
    private BaseQuickAdapter<String, BaseViewHolder> ${lower_acname}Adapter;
		</#if>
	</#if>
<#else>
	private ${FragmentName} ${lower_fgmtname};
</#if>

<#if generatedDrawerFragment>
       private ${DrawerFragmentName} ${lower_drawername};
</#if>
    @Override
    public void initView(Bundle savedInstanceState) {
	
<#if injectionTitleBar>
        //初始化TitleView
        bindTitleView();
</#if>
<#if generatedDrawerFragment>
        //初始化侧滑菜单
        initDrawerLayout();
</#if>
		
<#if !generatedFragment>
		<#if injectionRecyclerView>
        //初始化SmartRefreshLayout
        bindSmartRefreshLayout();
        //初始化QuickAdapter
        bindAdapter();
		
        viewOnRefresh();</#if>
<#else>
		switchTo();
</#if>
		
    }
<#if !generatedFragment>
	<#if injectionRecyclerView>
    //初始化QuickAdapter
    private void bindAdapter() {
	// 初始化Recyclerview 的Adapter
	<#if injectionEntity>
		
   ${lower_acname}Adapter = new BaseQuickAdapter<${EntityName}, BaseViewHolder>(R.layout.activity_${layout_acname}_item) {
            @Override
            protected void convert(final BaseViewHolder viewHolder, final ${EntityName} item) {



                viewHolder.setOnClickListener(R.id.${layout_acname}_item_ll, new OnNoFastClickListener() {
                    @Override
                    protected void onNoDoubleClick(View view) {

                    }
                });

            }
        };
		<#else>
		${lower_acname}Adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.activity_${layout_acname}_item) {
            @Override
            protected void convert(final BaseViewHolder viewHolder, final String item) {



                viewHolder.setOnClickListener(R.id.${layout_acname}_item_ll, new OnNoFastClickListener() {
                    @Override
                    protected void onNoDoubleClick(View view) {

                    }
                });

            }
        };
		</#if>
       

        //开启Recyclerview Item的加载动画
        ${lower_acname}Adapter.openLoadAnimation();
        // 初始化Recyclerview配置
        ${lower_acname}RecyclerView.setLayoutManager(new LinearLayoutManager(context));
		
        ItemDecoration itemDecoration = new ItemDecoration(context, ImageUtils.dipToPx(context, 5), R.color.transparent);
        itemDecoration.setDirectionMode(ItemDecoration.DividerMode.HORIZONTAL);
        ${lower_acname}RecyclerView.addItemDecoration(itemDecoration);
        ${lower_acname}RecyclerView.setAdapter(${lower_acname}Adapter);
	}
	
    // 初始化SmartRefreshLayout
    private void bindSmartRefreshLayout() {
        ${lower_acname}SmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
               
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshlayout) {
                
                viewOnRefresh();
            }
        });
        //SmartRefreshLayout 刷新加载Header样式
        ${lower_acname}SmartRefreshLayout.setRefreshHeader(new ClassicsHeader(context));
        ${lower_acname}SmartRefreshLayout.setEnableLoadMore(true);
        ${lower_acname}SmartRefreshLayout.setEnableRefresh(true);
	}
	
	private void viewOnRefresh() {
	<#if injectionMVP>
        mPresenter.checkData("1");
		</#if>
    }</#if>
	
</#if>
	
	<#if generatedDrawerFragment>
    private void offNavView() {
        ${lower_acname}Drawer.closeDrawer(${lower_acname}NavView);
    }

    public void openNavView() {
        ${lower_acname}Drawer.openDrawer(${lower_acname}NavView);
    }

    private void onOffNavView() {
        boolean isDrawerOpen = ${lower_acname}Drawer.isDrawerOpen(${lower_acname}NavView);
        if (isDrawerOpen) {
            ${lower_acname}Drawer.closeDrawer(${lower_acname}NavView);
//            toolbarTitleView.setRightIcon(R.mipmap.ic_search);
        } else {
            ${lower_acname}Drawer.openDrawer(${lower_acname}NavView);
//            toolbarTitleView.setRightIcon(R.mipmap.ic_clear_white);
        }
    }

    // 侧滑菜单  inflateHeaderView 进来的布局要宽一些
    private void initDrawerLayout() {
        ${lower_acname}Drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {

            }

            @Override
            public void onDrawerOpened(@NonNull View view) {
                KeyboardUtils.hideSoftKeyboard(view);
                toolbarTitleView.setRightIcon(R.mipmap.ic_clear_white);
            }

            @Override
            public void onDrawerClosed(@NonNull View view) {
                KeyboardUtils.hideSoftKeyboard(view);
                toolbarTitleView.setRightIcon(R.mipmap.ic_search);
            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (${lower_drawername} != null) {
            transaction.hide(${lower_drawername});
        }
        if (${lower_drawername} == null) {
            ${lower_drawername} = ${DrawerFragmentName}.newInstance("fkzg");
            transaction.add(R.id.${layout_acname}_nav_view, ${lower_drawername});
        } else {
            transaction.show(${lower_drawername});
        }
        transaction.commitAllowingStateLoss();
    }</#if>
<#if generatedFragment>
    // Fragment 切换
    private void switchTo() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);
        //
        if (${lower_fgmtname} == null) {
            ${lower_fgmtname} =${FragmentName}.newInstance();
            transaction.add(R.id.${layout_acname}_framelayout, ${lower_fgmtname});
        } else {
            transaction.show(${lower_fgmtname});
        }
        transaction.commitAllowingStateLoss();
    }

    // 将所有的Fragment都置为隐藏状态。
    private void hideFragments(FragmentTransaction transaction) {
        if (${lower_fgmtname} != null) {
            transaction.hide(${lower_fgmtname});
        }
    }
</#if>
	
	
<#if injectionTitleBar>
	// 初始化TitleView
    private void bindTitleView() {
        toolbarTitleView.setTitle(R.string.title_${layout_acname});
		<#if generatedDrawerFragment>
        toolbarTitleView.setRightIcon(R.mipmap.ic_search);
        toolbarTitleView.setRightIconVisibility(true);
        toolbarTitleView.setRightOnClickListener(new OnNoFastClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                onOffNavView();
            }
        });</#if>
	}

</#if>
	
<#if !generatedFragment>	
	<#if injectionMVP>
		<#if injectionEntity>
	@Override
    public void resultData(${EntityName} data){
		<#if injectionRecyclerView>
	  //${lower_acname}Adapter.replaceData(data);</#if>
	}
		<#else>
	@Override
    public void resultData(String data){
		<#if injectionRecyclerView>
	  //${lower_acname}Adapter.replaceData(data);</#if>
	}
		</#if>
	
    @Override
    public void onErrorSuccess(int code, String message, boolean isSuccess, boolean showTips) {
	<#if injectionRecyclerView>
        ${lower_acname}SmartRefreshLayout.finishRefresh();
        ${lower_acname}SmartRefreshLayout.finishLoadMore();</#if>
       if (!isSuccess) {
            ToastTool.error(message);
       } else if (showTips && !DataUtils.isNullString(message)) {
            ToastTool.success(message);
       }
    }
	</#if>
</#if>

<#if generatedDrawerFragment>
    @Override
    public void onBackPressed() {
        boolean isDrawerOpen = ${lower_acname}Drawer.isDrawerOpen(${lower_acname}NavView);
        if (isDrawerOpen) {
            ${lower_acname}Drawer.closeDrawer(${lower_acname}NavView);
            return;
        }
        super.onBackPressed();
    }
</#if>
}
